$(document).ready(function() {

	 $("#completeBtn").click(function () {
		 $("form").attr('action',contextRoot+"/surveydetails/completeSurvey.html?surveyId="+$("#surveyid").val());
	 });
	 
	 $("#saveBtn").click(function () {
		 if(!checkSurvey() && !checkDateFieldsSurvey()){
			 $("form").attr('action',contextRoot+"/surveydetails/update.html?surveyId="+$("#surveyid").val()+"&questionId="+$("#questionid").val());
		 }
		 else{
			 return false;
		 } 
	 });
	 
	 $("#addBtn").click(function () {
		 if(!checkSurvey() && !checkDateFieldsSurvey() && !checkQuestion()){
			 $("form").attr('action',contextRoot+"/surveydetails/update.html?surveyId="+$("#surveyid").val()+"&questionId="+$("#questionid").val());
		 }
		 else{
			 return false;
		 } 
	 });
	 
	 $("#cancelBtn").click(function () {
		 $("form").attr('method',"GET");
		 $("form").attr('action',contextRoot+"/surveydetails/view.html");
	 });

	 $("#cancelsurveyBtn").click(function () {
		 $("form").attr('method',"GET");
		 $("form").attr('action',contextRoot+"/surveyordetails/view.html");
	 });
	 
	 $("#previousBtn").click(function () {
		 if(!checkform()){
			 $("#surveyAnswersVO_previous").val("Y");	
			 $("form").attr('method',"GET");
			 $("form").attr('action',contextRoot+"/surveyordetails/update.html");
		 }
		 else{
			 return false;
		 } 		 
	 });
	 $("#nextBtn").click(function () {
		 if(!checkform()){
			 $("#surveyAnswersVO_previous").val("N");	
			 $("form").attr('method',"GET");
			 $("form").attr('action',contextRoot+"/surveyordetails/update.html");
		 }
		 else{
			 return false;
		 } 		 
	 });
	 $("#finishBtn").click(function () {
		 if(!checkform()){
			 $("#surveyAnswersVO_finish").val("Y");	
			 $("form").attr('method',"GET");
			 $("form").attr('action',contextRoot+"/surveyordetails/update.html");
		 }
		 else{
			 return false;
		 } 		 
	 });
	 
	});

function checkSurvey() {
	$(".error").hide();
	var hasError = false;
	var surveyName = $("#surveyName").val();	
	if (surveyName == "") {
		$("#validation")
				.after(
						'<span class="error" style="color: red">Survey Name is mandatory.</span>');
		hasError = true;
	}
	return hasError;
}

function checkDateFieldsSurvey(){
	$(".error").hide();
	var hasError = false;		
	var fromDate = $("#fromdt").val();	
	var toDate = $("#todt").val();	
	if (fromDate == "" || toDate == "") {
		$("#validation")
				.after(
						'<span class="error" style="color: red">Date fields are mandatory</span>');
		hasError = true;
	}else if (Date.parse(fromDate) >= Date.parse(toDate)) {
		$("#validation")
		.after(
				'<span class="error" style="color: red">Start Date must be lesser than End Date</span>');
		hasError = true;
	}
	return hasError;
}

function checkQuestion() {
	$(".error").hide();
	var hasError = false;
	var quesDesc = $("#quesDesc").val();	
	if (quesDesc == "") {
		$("#validation")
				.after(
						'<span class="error" style="color: red">Survey Question is mandatory.</span>');
		hasError = true;
	}
	return hasError;
}

function checkform() { 
	$(".error").hide();
	var hasError = false;
	var isAnswerNotAvlb = false;
	var names = {};
	$('input:radio').each(function() { // find unique names
		var radioName = $(this).attr('name');
		var radioValue = $(this).attr('value');
		var radioStatus = $(this).attr('checked');
		if(radioValue == "0" && radioStatus == true){
			var name =$(this).attr('name');
			var array = name.split(/[\s.]+/);
			var answerDesc = document.getElementsByName(array[0]+".answerdescription")[0].value;
			if(answerDesc==""){
				isAnswerNotAvlb = true;				
			}
		}
	    names[$(this).attr('name')] = true;
	});
	var count = 0;
	$.each(names, function() { // then count them
       count++;
	});
	if($('input:radio:checked').length == count) {
		hasError = false;
	}else{
		$("#validation")
		.after(
				'<span class="error" style="color: red">Please answer all the questions</span>');
		hasError = true;
	}
	if(isAnswerNotAvlb){
		$("#validation")
		.after(
				'<span class="error" style="color: red">Please enter a comment for Not Applicable Questions</span>');
		hasError = true;
	}
	return hasError;
} 